package cn.yoline.xylink.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import cn.yoline.xylink.R;

/**
 * Copyright 2020 JxGIS
 *
 * @file MoreItemDialog
 * @auth linaisheng
 * Created on 2022/7/28.
 * Description：
 */
public class MoreItemDialog extends BottomSheetDialog {


    public enum MoreItemType {
        Whiteboard,
        Screenshot,
        ShareScreen
    }

    private boolean ableWhiteboard;
    private boolean ableScreenshot;
    private OnItemClickListener itemClickListener;

    public MoreItemDialog(
            @NonNull Context context, boolean ableWhiteboard,
            boolean ableShareScreed, boolean ableScreenshot,
            OnItemClickListener clickListener) {
        super(context);
        View rootView = LayoutInflater.from(context).inflate(R.layout.dialog_more_item, null);
        setContentView(rootView);

        this.ableWhiteboard = ableWhiteboard;
        this.ableScreenshot = ableScreenshot;
        itemClickListener = clickListener;

        LinearLayout llWhiteboard = rootView.findViewById(R.id.ll_item_whiteboard);
        ImageView ivWhiteboard = rootView.findViewById(R.id.iv_item_whiteboard);
        TextView tvWhiteboard = rootView.findViewById(R.id.tv_item_whiteboard);
        ivWhiteboard.setEnabled(ableWhiteboard);
        tvWhiteboard.setEnabled(ableWhiteboard);

        LinearLayout llScreenshot = rootView.findViewById(R.id.ll_item_screenshot);
        ImageView ivScreenshot = rootView.findViewById(R.id.iv_item_screenshot);
        TextView tvScreenshot = rootView.findViewById(R.id.tv_item_screenshot);
        ivScreenshot.setEnabled(ableScreenshot);
        tvScreenshot.setEnabled(ableScreenshot);

        LinearLayout llShareScreen = rootView.findViewById(R.id.ll_item_share_screen);
        ImageView ivShareScreen = rootView.findViewById(R.id.iv_item_share_screen);
        TextView tvShareScreen = rootView.findViewById(R.id.tv_item_share_screen);
        ivShareScreen.setEnabled(ableShareScreed);
        tvShareScreen.setEnabled(ableShareScreed);


        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClickListener != null) {
                    dealClickItem(view);
                }
            }
        };
        llWhiteboard.setOnClickListener(listener);
        llScreenshot.setOnClickListener(listener);
        llShareScreen.setOnClickListener(listener);


        BottomSheetBehavior mDialogBehavior = BottomSheetBehavior.from((View) rootView.getParent());
        mDialogBehavior.setPeekHeight(getPeekHeight(context));
    }

    private int getPeekHeight(Context context) {
        int peekHeight = context.getResources().getDisplayMetrics().heightPixels;
        //设置弹窗高度为屏幕高度的1/3
        return peekHeight / 3;
    }

    private void dealClickItem(View view) {
        if (view.getId() == R.id.ll_item_whiteboard && ableWhiteboard) {
            itemClickListener.onItemClick(MoreItemType.Whiteboard);
            dismiss();
        } else if (view.getId() == R.id.ll_item_screenshot && ableScreenshot) {
            itemClickListener.onItemClick(MoreItemType.Screenshot);
            dismiss();
        } else if (view.getId() == R.id.ll_item_share_screen) {
            itemClickListener.onItemClick(MoreItemType.ShareScreen);
            dismiss();
        }
    }

    public interface OnItemClickListener{
        void onItemClick(MoreItemType itemType);
    }



}